package top.jolyoulu.sql

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

/**
 * @Author: JolyouLu
 * @Date: 2024/5/16 19:21
 * @Description
 */
object Spark01_SparkSQL_Basic {
  def main(args: Array[String]): Unit = {
    //创建SparkSQL的运行环境
    val sparkSQL: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")
    val spark = SparkSession.builder().config(sparkSQL).getOrCreate()
    val sc: SparkContext = spark.sparkContext
    import spark.implicits._ //将spark转换规则引入
    //执行逻辑操作
    //DataFrame
    println("======================DataFrame Read======================")
    val path: String = this.getClass.getClassLoader.getResource("datas/user.json").toURI.getPath
    val df: DataFrame = spark.read.json(path)
    df.show()
    //SQL
    println("======================DataFrame SQL======================")
    df.createOrReplaceTempView("user")
    spark.sql("select * from user").show()
    spark.sql("select age,username from user").show()
    spark.sql("select avg(age) from user").show()
    //DSL
    println("======================DataFrame DSL======================")
    df.select("age","username").show
    df.select('age + 1).show
    
    //关闭环境
    spark.close()
  }
  case class User(age:Long,username:String)
}
